package action;

import static com.sun.corba.se.spi.presentation.rmi.StubAdapter.request;
import controller.Action;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Aluno;
import model.Avaliacao;
import observer.Observer;
import dao.AvaliacaoDao;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConsultarNotaFrequenciaAction extends Action implements Observer{

    private static HttpServletRequest request;
    private static HttpServletResponse response;
    
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String acao = request.getParameter("acao");
        try {

            int matricula = Integer.parseInt(request.getParameter("matricula"));
            if (acao.equals("prepararOperacao")) {
                List<Avaliacao> avaliacoes = Avaliacao.obterAvaliacoesPorAluno(matricula);
                request.setAttribute("avaliacoes", avaliacoes);
            } else {
                if (acao.equals("confirmarOperacao")) {
                    String pesqisa = request.getParameter("pesquisa");
                    if (pesqisa.equals("Todas")) {
                        List<Avaliacao> avaliacoes = Avaliacao.obterAvaliacoesPorAluno(matricula);
                        request.setAttribute("avaliacoes", avaliacoes);
                    } else {
                        int ano = Integer.parseInt(request.getParameter("ano"));
                        int semestre = Integer.parseInt(request.getParameter("semestre"));
                        List<Avaliacao> avaliacoes = Avaliacao.obterAvaliacoesPorAnoSemestre(ano, semestre, matricula);
                        request.setAttribute("avaliacoes", avaliacoes);
                    }
                }
            }

            Aluno aluno = Aluno.obterAluno(matricula);
            request.setAttribute("aluno", aluno);

            RequestDispatcher janela = request.getRequestDispatcher("/consultarNotaFrequencia.jsp");
            janela.forward(request, response);
            
            this.request = request;
            this.response = response;
            AvaliacaoDao.getInstence().attach(this);
            
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void prepararOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    }

    @Override
    public void confirmarOperacao(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    }
    
    @Override
    public void update(){
        System.out.println("\nNota lançada.\n");
    }

}
